import { dayjs } from 'element-plus'

/**
 * @param {Date | number | string} time 需要转换的时间
 * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
 */
export function formatDate(date: Date | number | string, fmt: string = 'YYYY-MM-DD'): string {
  return dayjs(date).format(fmt)
}

export function stringToHex(inputString: string): string {
  return inputString
    .split('')
    .map((char) => char.charCodeAt(0).toString(16).padStart(2, '0'))
    .join('')
}

export function hexToString(hexStr: string): string {
  let str = ''
  for (let i = 0; i < hexStr.length; i += 2) {
    const hex = hexStr.slice(i, i + 2)
    str += String.fromCharCode(parseInt(hex, 16))
  }
  return str
}
